package com.freshfresh.activity.selfcenter;

import java.util.HashMap;
import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.activity.home.HomeActivity;
import com.freshfresh.utils.DeviceUtil;
import com.freshfresh.utils.RegexPattern;
import com.freshfresh.utils.StringUtil;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;

//注册
public class RegisterActivity extends BaseActivity implements OnClickListener{

	private Button btn_verificationCode;
	private Button btn_regiserGo;
	private EditText ed_verificationCode;
	private EditText ed_phoneNumber;
	private EditText ed_password;
	private EditText ed_comfimedPassword;
	private String phoneNumber;
	private TextView tv_register;
	private ImageView iv_back;
	private String password;
	Dialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_register);
		initView();
	}

	public void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_ac_register_back);
		btn_verificationCode = (Button) findViewById(R.id.btn_register_verification_code);
		btn_regiserGo = (Button) findViewById(R.id.btn_ac_register_go);

		ed_phoneNumber = (EditText) findViewById(R.id.ed_ac_register_phone_number);
		ed_verificationCode = (EditText) findViewById(R.id.ed_ac_register_verification_code);
		ed_password = (EditText) findViewById(R.id.ed_ac_register_password);
		ed_comfimedPassword = (EditText) findViewById(R.id.ed_ac_register__confimed_password);

		tv_register = (TextView) findViewById(R.id.tv_ac_register_login);

		iv_back.setOnClickListener(this);
		btn_verificationCode.setOnClickListener(this);
		btn_regiserGo.setOnClickListener(this);

		tv_register.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.iv_ac_register_back:
			this.finish();
			break;
		case R.id.btn_register_verification_code:
			requestVerificationCode();
			break;
		case R.id.btn_ac_register_go:
			register();
			break;
		case R.id.tv_ac_register_login:
			Intent it = new Intent(this, LoginActivity.class);
			this.startActivity(it);
			break;
		default:
			break;
		}
	}

	/**
	 * 发送验证码
	 */
	protected void requestVerificationCode() {
		phoneNumber = ed_phoneNumber.getText().toString();
		if (!RegexPattern.isPhone(phoneNumber)) {
			showToast(RegisterActivity.this, "请输入正确的手机号码");
			return;
		}
		progressDialog = Utils.createLoadingDialog(RegisterActivity.this,
				"验证码发送中……");
		progressDialog.show();

		Map<String, String> params = new HashMap<String, String>();
		params.put("mobile", phoneNumber);
		executeRequest(new StringRequest(UrlConstants.verification_code,
				params, new Response.Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							showToast(RegisterActivity.this, "验证码已发送，请注意查收");
						} else {
							showToast(RegisterActivity.this, "获取验证码失败");
						}
					}
				}, null));

	}

	protected void register() {
		String verificationCode = ed_verificationCode.getText().toString();
		password = ed_password.getText().toString();
		String comfimedPassword = ed_comfimedPassword.getText().toString();
		phoneNumber = ed_phoneNumber.getText().toString();

		if (!RegexPattern.isPhone(phoneNumber)) {
			showToast(RegisterActivity.this, "请输入正确的手机号码");
			return;
		}
		if (StringUtil.isBlank(verificationCode)) {
			showToast(RegisterActivity.this, "请输入验证码");
			return;
		}
		if (!password.equals(comfimedPassword)) {
			showToast(RegisterActivity.this, "两次密码不一致，请重新输入");
			return;
		}
		progressDialog = Utils.createLoadingDialog(RegisterActivity.this,
				"注册中……");
		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		params.put("mobile", phoneNumber);
		params.put("password", password);
		params.put("verifycode", verificationCode);
		params.put("ostype", "android");
		params.put("uuid", DeviceUtil.getMyUUID(RegisterActivity.this));
		params.put("method", "mobile");

		executeRequest(new StringRequest(UrlConstants.user_register, params,
				new Response.Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						progressDialog.dismiss();

						if (!Utils.parseJsonStr(arg0).get("errorcode")
								.equals("0")) {
							String errorCode = Utils.parseJsonStr(arg0)
									.get("errorcode").toString();
							Log.e("errorcode", errorCode);
							if (errorCode.equals("Phone number invalid !")) {
								showToast(RegisterActivity.this, "注册失败，"
										+ "该手机号已经注册,请直接登录");

								Intent intent = new Intent(
										RegisterActivity.this,
										HomeActivity.class);
								startActivity(intent);
							} else {
								showToast(RegisterActivity.this, "注册失败,"
										+ "该手机号已经注册,请直接登录");
							}
						} else {
							showToast(RegisterActivity.this, "注册成功");
							SharedPreferences sp = getSharedPreferences(
									"config", Context.MODE_PRIVATE);
							Editor editor = sp.edit();
							editor.putString("mobile", phoneNumber);
							editor.putString("password", password);
							editor.putString("token", Utils.parseJsonStr(arg0)
									.get("token").toString());
							editor.commit();

							Map<String, Object> map = (Map<String, Object>) Utils
									.parseJsonStr(arg0).get("userinfo");

							String user_id = map.get("user_id").toString();
							String mobile = map.get("mobile").toString();
							System.out.println("userid=" + user_id + mobile);

							Intent intent = new Intent(RegisterActivity.this,
									HomeActivity.class);
							startActivity(intent);
						}
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {

					}
				}));
	}
}
